package com.plugin.mybatis.generator.plus;

import org.mybatis.generator.api.*;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.internal.ObjectFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;

public class GeneralServiceCodePlugin extends PluginAdapter {
	public boolean validate(List<String> warnings) {
		return true;
	}
	@Override
	public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles() {
		return super.contextGenerateAdditionalJavaFiles();
	}

	public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
		List<GeneratedJavaFile> javafiles = new ArrayList<>();


		List<GeneratedJavaFile> getGenFiles = introspectedTable.getGeneratedJavaFiles();

		properties.get("targetProject");
		properties.get("targetPackage");
		properties.get("implTargetProject");
		properties.get("implTargetPackage");
		properties.get("clientTargetProject");
		properties.get("clientTargetPackage");
		properties.get("controllerTargetProject");
		properties.get("controllerTargetPackage");


		String targetProject = this.getProperties().getProperty("targetProject");
		String targetPackage =this.getProperties().getProperty("targetPackage");
		String implTargetProject = this.getProperties().getProperty("implTargetProject");
		String implTargetPackage =this.getProperties().getProperty("implTargetPackage");
		String clientTargetProject = this.getProperties().getProperty("clientTargetProject");
		String clientTargetPackage =this.getProperties().getProperty("clientTargetPackage");

		String controllerTargetProject = this.getProperties().getProperty("controllerTargetProject");
		String controllerTargetPackage = this.getProperties().getProperty("controllerTargetPackage");

		//String serviceRootInterface =this.getProperties().getProperty("serviceRootInterface");

		String poClassName =introspectedTable.getTableConfiguration().getProperties().getProperty("poName");
		//String poMapperInterFaceName =introspectedTable.getTableConfiguration().getProperties().getProperty("poMapper");
		String poMapperInterFaceName =poClassName + "Mapper";

		if(!stringHasValue(poClassName)){
			poClassName = "PO实体";
		}
		if(!stringHasValue(poMapperInterFaceName)){
			poMapperInterFaceName ="实体Mapper";
		}
		JavaFormatter javaFormatter = ObjectFactory.createJavaFormatter(context) ;

		FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();

		String dtoModelTargetPackage = context.getJavaModelGeneratorConfiguration().getTargetPackage();

		//response DTO 生成
		//当前DTO目录下的response包下。//TODO 后续定成参数。
		String resDtoModelTargetPackage = context.getJavaModelGeneratorConfiguration().getTargetPackage()+"."+"response";
		String resDtoName = "Res"+table.getDomainObjectName().replace("Req","");
		String resDtoImport = resDtoModelTargetPackage + "." + resDtoName;
		if(null != getGenFiles && getGenFiles.size() > 0){
			if(!haveFile(targetProject,resDtoModelTargetPackage+"."+resDtoName)){
				GeneratedJavaFile asa = copyDTOResponse(introspectedTable,getGenFiles.get(0),targetProject,resDtoModelTargetPackage,resDtoName,table.getDomainObjectName(),javaFormatter);
				javafiles.add(asa);
			}
		}

		//query DTO 生成
		//当前DTO目录下的query包下。//TODO 后续定成参数。
		String queryDtoModelTargetPackage = context.getJavaModelGeneratorConfiguration().getTargetPackage()+"."+"query";
		String queryDtoName = table.getDomainObjectName().replace("DTO","QueryDTO").replace("Vo","QueryVo");
		String queryDtoImport = queryDtoModelTargetPackage + "." + queryDtoName;
		if(null != getGenFiles && getGenFiles.size() > 0){
			if(!haveFile(targetProject,queryDtoModelTargetPackage+"."+queryDtoName)){
				GeneratedJavaFile asa = copyDtoQuery(introspectedTable,getGenFiles.get(0),targetProject,queryDtoModelTargetPackage,queryDtoName,javaFormatter);
				javafiles.add(asa);
			}
		}


		//Facade 接口
		String facadeName = table.getDomainObjectName().replace("DTO","").replace("Vo","") + "Service";
		String interPage = targetPackage + "." + facadeName;
		FullyQualifiedJavaType facadeType = new FullyQualifiedJavaType(interPage);

		//facadeImpl 文件
		String facadeImplPage = implTargetPackage+"."+facadeName+"Impl";
		//client 文件
		String clientPage = clientTargetPackage+"."+table.getDomainObjectName().replace("DTO","").replace("Vo","")+"Client";

		//controller 文件
		String controllerPage = controllerTargetPackage+"."+table.getDomainObjectName().replace("DTO","").replace("Vo","")+"Controller";
		boolean facadeHave = false;
		boolean facadeImplHave = false;
		boolean clientHave = false;
		boolean controllerHave = false;

		if(haveFile(targetProject,interPage)){
			facadeHave = true;
		}

		if(haveFile(implTargetProject,facadeImplPage)){
			facadeImplHave = true;
		}
		if(haveFile(clientTargetProject,clientPage)){
			clientHave = true;
		}
		if(haveFile(controllerTargetProject,controllerPage)){
			controllerHave = true;
		}
		if(!facadeHave){
			Interface interfaze = new Interface(facadeType);
			interfaze.setVisibility(JavaVisibility.PUBLIC);
			interfaze.addImportedType(new FullyQualifiedJavaType(dtoModelTargetPackage+"."+table.getDomainObjectName()));
			interfaze.addImportedType(new FullyQualifiedJavaType(resDtoImport));
			interfaze.addImportedType(new FullyQualifiedJavaType(queryDtoImport));
			interfaze.addImportedType(new FullyQualifiedJavaType("io.swagger.annotations.Api"));
			interfaze.addImportedType(new FullyQualifiedJavaType("java.util.List"));
			interfaze.addImportedType(new FullyQualifiedJavaType("io.swagger.annotations.ApiOperation"));
			interfaze.addImportedType(new FullyQualifiedJavaType("org.springframework.web.bind.annotation.*"));
			interfaze.addImportedType(new FullyQualifiedJavaType("com.myt.service.core.entity.ApiResponse"));

			//新增方法
			Method create = new Method();
			create.addJavaDocLine("/**");
			create.addJavaDocLine(" *  MBG.service ");
			create.addJavaDocLine(" * 创建-C" );
			create.addJavaDocLine(" */");
			create.setVisibility(JavaVisibility.PUBLIC);
			create.setName("create");
			create.addParameter(new Parameter(new FullyQualifiedJavaType(""+ table.getDomainObjectName()), "vo"));
			create.setReturnType(new FullyQualifiedJavaType("ApiResponse"));
			interfaze.addMethod(create);

			//修改方法
			Method update = new Method();
			update.addJavaDocLine("/**");
			update.addJavaDocLine(" * MBG.service ");
			update.addJavaDocLine(" * 根据条件修改-U" );
			update.addJavaDocLine(" */");
			update.setVisibility(JavaVisibility.PUBLIC);
			update.setName("update");
			update.addParameter(new Parameter(new FullyQualifiedJavaType(""+ table.getDomainObjectName()), "vo"));
			update.setReturnType(new FullyQualifiedJavaType("ApiResponse"));
			interfaze.addMethod(update);

			//删除方法
			Method remove = new Method();
			remove.addJavaDocLine("/**");
			remove.addJavaDocLine(" * MBG.service ");
			remove.addJavaDocLine(" * 通过主键Id删除-D" );
			remove.addJavaDocLine(" */");
			remove.setVisibility(JavaVisibility.PUBLIC);
			remove.setName("delete");
			remove.addParameter(new Parameter(new FullyQualifiedJavaType("Integer"), "id"));
			remove.addParameter(new Parameter(new FullyQualifiedJavaType("String"), "updateBy"));
			remove.setReturnType(new FullyQualifiedJavaType("ApiResponse"));
			interfaze.addMethod(remove);

			//通过ID查询
			Method get = new Method();
			get.addJavaDocLine("/**");
			get.addJavaDocLine(" * MBG.service ");
			get.addJavaDocLine(" * 通过主键Id查询" );
			get.addJavaDocLine(" */");
			get.setVisibility(JavaVisibility.PUBLIC);
			get.setName("getById");
			get.addParameter(new Parameter(new FullyQualifiedJavaType("Integer"), "id"));
			get.setReturnType(new FullyQualifiedJavaType("ApiResponse<"+resDtoName+">"));
			interfaze.addMethod(get);


			//查询所有
//			Method list = new Method();
//			list.addJavaDocLine("/**");
//			list.addJavaDocLine(" * MBG.service ");
//			list.addJavaDocLine(" * 查询所有" );
//			list.addJavaDocLine(" */");
//			list.setVisibility(JavaVisibility.PUBLIC);
//			list.setName("list");
//			list.setReturnType(new FullyQualifiedJavaType("BaseListResult<"+resDtoName+">"));
//			interfaze.addMethod(list);


			//条件查询所有列表
			Method findList = new Method();
			findList.addJavaDocLine("/**");
			findList.addJavaDocLine(" * MBG.service ");
			findList.addJavaDocLine(" * 条件查询所有" );
			findList.addJavaDocLine(" */");
			findList.setVisibility(JavaVisibility.PUBLIC);
			findList.setName("findList");
			findList.addParameter(new Parameter(new FullyQualifiedJavaType(queryDtoName), "vo"));
			findList.setReturnType(new FullyQualifiedJavaType("ApiResponse<List<"+resDtoName+">>"));
			interfaze.addMethod(findList);

			//条件分页查询
			Method findPage = new Method();
			findPage.addJavaDocLine("/**");
			findPage.addJavaDocLine(" * MBG.service ");
			findPage.addJavaDocLine(" * 条件分页查询" );
			findPage.addJavaDocLine(" */");
			findPage.setVisibility(JavaVisibility.PUBLIC);
			findPage.setName("findPage");
			findPage.addParameter(new Parameter(new FullyQualifiedJavaType("PageQuery<"+queryDtoName+">"), "vo"));
			//findPage.addParameter(new Parameter(new FullyQualifiedJavaType("Integer"), "currentPage"));
			//findPage.addParameter(new Parameter(new FullyQualifiedJavaType("Integer"), "pageSize"));
			findPage.setReturnType(new FullyQualifiedJavaType("ApiResponse<PageInfo<"+resDtoName+">>"));
			interfaze.addMethod(findPage);


			//2020年11月20日09:27:24 .add
//			FullyQualifiedJavaType managerInterfaceType = new FullyQualifiedJavaType(serviceRootInterface+"<"+table.getDomainObjectName().replace("DTO","").replace("Vo","")+">");
//			interfaze.addSuperInterface(managerInterfaceType);
			//2020年11月20日09:27:24 .add

			GeneratedJavaFile generatedJavafaceFile = new GeneratedJavaFile(interfaze, targetProject, javaFormatter);
			javafiles.add(generatedJavafaceFile);
		}




		//facade实现文件生成
		if(stringHasValue(implTargetProject) && stringHasValue(implTargetPackage)){
			if(!facadeImplHave){
				GeneratedJavaFile generatedJavaImplFile = generatedJavaImplFile(null,queryDtoImport,queryDtoName,resDtoImport,resDtoName,facadeName,interPage,implTargetProject
						,implTargetPackage,table,facadeType,introspectedTable,poMapperInterFaceName
						,poClassName,javaFormatter);
				javafiles.add(generatedJavaImplFile);
			}
			//client文件生成
			/*if(stringHasValue(clientTargetProject) && stringHasValue(clientTargetPackage)){
				if(!clientHave){
					GeneratedJavaFile generatedJavaClientFile = generatedJavaClientFile(queryDtoImport,queryDtoName,resDtoImport,resDtoName,facadeName,interPage,clientTargetProject
							,clientTargetPackage,table,facadeType,introspectedTable,poMapperInterFaceName
							,poClassName,javaFormatter);
					javafiles.add(generatedJavaClientFile);
				}
				if(stringHasValue(controllerTargetProject) && stringHasValue(controllerTargetPackage)){
					if(!controllerHave){
						GeneratedJavaFile generatedJavaControllerFile = generatedJavaControllerFile(queryDtoImport,queryDtoName,resDtoImport,resDtoName,interPage,controllerTargetProject
								,controllerTargetPackage,table,clientTargetPackage,introspectedTable,poMapperInterFaceName,poClassName,javaFormatter);
						javafiles.add(generatedJavaControllerFile);
					}
				}
			}*/
			if(stringHasValue(controllerTargetProject) && stringHasValue(controllerTargetPackage)){
				if(!controllerHave){
					GeneratedJavaFile generatedJavaControllerFile = generatedJavaControllerFile(queryDtoImport,queryDtoName,resDtoImport,resDtoName,interPage,controllerTargetProject
							,controllerTargetPackage,table,clientTargetPackage,introspectedTable,poMapperInterFaceName,poClassName,javaFormatter);
					javafiles.add(generatedJavaControllerFile);
				}
			}
		}


		return javafiles;
	}

	/**
	 *service 实现文件生成
	 * @return
	 */
	public GeneratedJavaFile generatedJavaImplFile(String serviceRootInterface,String queryDtoImport, String queryDtoName, String resDtoImport, String resDtoName, String facadeName, String interPage, String implTargetProject
			, String implTargetPackage, FullyQualifiedTable table, FullyQualifiedJavaType facadeType
			, IntrospectedTable introspectedTable, String poMapperInterFaceName, String poClassName, JavaFormatter javaFormatter){
		//######################################facade 实现类 ######################################
		interPage = implTargetPackage+"."+facadeName+"Impl";
		FullyQualifiedJavaType typeImp = new FullyQualifiedJavaType(interPage);
		TopLevelClass topLevelClass = new TopLevelClass(typeImp);
		topLevelClass.setVisibility(JavaVisibility.PUBLIC);
		topLevelClass.addSuperInterface(facadeType);

		//2020年11月20日09:36:55 .add
		FullyQualifiedJavaType managerImplInterfaceType = new FullyQualifiedJavaType(serviceRootInterface+"Impl"+"<"+table.getDomainObjectName().replace("DTO","").replace("Vo","")+">");
		topLevelClass.addImportedType(serviceRootInterface + "Impl");
		//topLevelClass.setSuperClass(managerImplInterfaceType);
		//2020年11月20日09:36:55 .add

		topLevelClass.addImportedType(facadeType);
		topLevelClass.addImportedType(new FullyQualifiedJavaType(resDtoImport));
		topLevelClass.addImportedType(new FullyQualifiedJavaType(queryDtoImport));
		topLevelClass.addImportedType("org.springframework.beans.factory.annotation.Autowired");
		topLevelClass.addImportedType(new FullyQualifiedJavaType("java.util.List"));
		//topLevelClass.addImportedType(new FullyQualifiedJavaType("org.springframework.web.bind.annotation.RequestMapping"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("com.myt.service.utils.*"));
		//topLevelClass.addImportedType(new FullyQualifiedJavaType("tk.mybatis.mapper.entity.Example"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("org.springframework.stereotype.Service"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("com.github.pagehelper.PageHelper"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("com.github.pagehelper.PageInfo"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("java.time.LocalDateTime"));
		//topLevelClass.addImportedType(new FullyQualifiedJavaType("tk.mybatis.mapper.weekend.Weekend"));
		//topLevelClass.addImportedType(new FullyQualifiedJavaType("tk.mybatis.mapper.weekend.WeekendCriteria"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("org.apache.commons.lang3.StringUtils"));

		topLevelClass.addJavaDocLine("/**");
		topLevelClass.addJavaDocLine(" * MBG.ServiceImpl ");
		topLevelClass.addJavaDocLine(" */");
		topLevelClass.addAnnotation("@Service");
		FullyQualifiedJavaType parameterType =  new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
		topLevelClass.addImportedType(parameterType);

		//注入当前Mapper
		Field field = new Field();
		field.addAnnotation("@Autowired");
		field.setVisibility(JavaVisibility.PRIVATE);
		FullyQualifiedJavaType daoInfaceType = new FullyQualifiedJavaType(poMapperInterFaceName);
		field.setType(daoInfaceType);
		String shortName = "thisMapper";
		field.setName(shortName);
		topLevelClass.addField(field );

		topLevelClass.addImportedType(FullyQualifiedJavaType.getNewMapInstance());


//		Method method = new Method();
//		method = new Method();
//		method.addAnnotation("@Autowired");
//		method.setVisibility(JavaVisibility.PUBLIC);
//		method.setName(table.getDomainObjectName()+"ServiceImpl");
//		method.addParameter(new Parameter(daoInfaceType, shortName));
//		method.addBodyLine("this.mapper = "+shortName+";");
//		method.addBodyLine("this.thisMapper = "+shortName+";");
//		topLevelClass.addMethod(method );


		//新增方法
		Method createImpl = new Method();
		createImpl.addJavaDocLine("/**");
		createImpl.addJavaDocLine(" *  MBG.serviceImpl ");
		createImpl.addJavaDocLine(" * 新增" );
		createImpl.addJavaDocLine(" */");
		createImpl.addAnnotation("@Override");
		createImpl.addBodyLine("ApiResponse baseObjectResult = ApiResponse.ok();");
		createImpl.addBodyLine(poClassName+" poModel = BeanMapper.map(vo,"+poClassName+".class);");
		createImpl.addBodyLine("poModel.setId(SnowflakeIdWorker.generateId());");
		createImpl.addBodyLine("poModel.setCreateTime(LocalDateTime.now());");
		createImpl.addBodyLine("poModel.setDelFlag(0);");
		createImpl.addBodyLine("//TODO 有操作者信息模型设置操作人信息");
		createImpl.addBodyLine("int count = "+shortName+".insert(poModel);");
		createImpl.addBodyLine("if (count > 0) {");
		createImpl.addBodyLine("return baseObjectResult;");
		createImpl.addBodyLine("}");
		createImpl.addBodyLine("return ApiResponse.fail(\"新增数据失败\");");
		createImpl.setVisibility(JavaVisibility.PUBLIC);
		createImpl.setName("create");
		createImpl.addParameter(new Parameter(new FullyQualifiedJavaType(""+table.getDomainObjectName()), "vo"));
		createImpl.setReturnType(new FullyQualifiedJavaType("ApiResponse"));
		topLevelClass.addMethod(createImpl);

		//修改方法
		Method updateImpl = new Method();
		updateImpl.addJavaDocLine("/**");
		updateImpl.addJavaDocLine(" * MBG.serviceImpl ");
		updateImpl.addJavaDocLine(" * 修改" );
		updateImpl.addJavaDocLine(" */");
		updateImpl.addAnnotation("@Override");
		updateImpl.addBodyLine(poClassName+" poModel = BeanMapper.map(vo,"+poClassName+".class);");
		updateImpl.addBodyLine("//根据业务需求写更新条件，可以是主键id，也可以是业务id，或者其他");
		updateImpl.addBodyLine("int count = "+shortName+".updateById(poModel);");
		updateImpl.addBodyLine("return ApiResponse.ok(\"修改成功\" + count + \"条数据受影响\");");
		updateImpl.setVisibility(JavaVisibility.PUBLIC);
		updateImpl.setName("update");
		updateImpl.addParameter(new Parameter(new FullyQualifiedJavaType(""+table.getDomainObjectName()), "vo"));
		updateImpl.setReturnType(new FullyQualifiedJavaType("ApiResponse"));
		topLevelClass.addMethod(updateImpl);

		//删除方法
		Method removeImpl = new Method();
		removeImpl.addJavaDocLine("/**");
		removeImpl.addJavaDocLine(" * MBG.serviceImpl ");
		removeImpl.addJavaDocLine(" * 通过ID删除" );
		removeImpl.addJavaDocLine(" */");
		removeImpl.addAnnotation("@Override");
		removeImpl.addBodyLine(poClassName+" poModel = new "+poClassName+"();");
		removeImpl.addBodyLine("poModel.setId(id);");
		removeImpl.addBodyLine("poModel.setDelFlag(1);");
		removeImpl.addBodyLine("//TODO 根据需求写业务 是物理删除还是逻辑删除，这是一个逻辑删除示例 通过id更新某字段为 删除状态");
		removeImpl.addBodyLine("//TODO 物理删除使用 thisMapper.updateById(id) ");
		removeImpl.addBodyLine("int count = "+shortName+".updateById(poModel);");
		removeImpl.addBodyLine("return ApiResponse.ok(\"删除成功\" + count + \"条数据受影响\");");
		removeImpl.setVisibility(JavaVisibility.PUBLIC);
		removeImpl.setName("delete");
		removeImpl.addParameter(new Parameter(new FullyQualifiedJavaType("Integer"), "id"));
		removeImpl.addParameter(new Parameter(new FullyQualifiedJavaType("String"), "updateBy"));
		removeImpl.setReturnType(new FullyQualifiedJavaType("ApiResponse"));
		topLevelClass.addMethod(removeImpl);

		//通过ID查询
		Method getImpl = new Method();
		getImpl.addJavaDocLine("/**");
		getImpl.addJavaDocLine(" * MBG.serviceImpl ");
		getImpl.addJavaDocLine(" * 通过ID查询" );
		getImpl.addJavaDocLine(" */");
		getImpl.addAnnotation("@Override");
		getImpl.addBodyLine("ApiResponse<"+resDtoName+"> baseObjectResult = ApiResponse.ok();");
		getImpl.addBodyLine(poClassName+" poModel = "+shortName+".selectById(id);");
		getImpl.addBodyLine(resDtoName+" thisDTO = BeanMapper.map(poModel,"+resDtoName+".class);");
		getImpl.addBodyLine("baseObjectResult.setData(thisDTO);");
		getImpl.addBodyLine("return baseObjectResult;");
		getImpl.setVisibility(JavaVisibility.PUBLIC);
		getImpl.setName("getById");
		getImpl.addParameter(new Parameter(new FullyQualifiedJavaType("String"), "id"));
		getImpl.setReturnType(new FullyQualifiedJavaType("ApiResponse<"+resDtoName+">"));
		topLevelClass.addMethod(getImpl);


		Method listImpl = new Method();
		listImpl.addJavaDocLine("/**");
		listImpl.addJavaDocLine(" * MBG.service ");
		listImpl.addJavaDocLine(" * 查询所有" );
		listImpl.addJavaDocLine(" */");
		//listImpl.addAnnotation("@Override");
		listImpl.addBodyLine("ApiResponse<List<"+resDtoName+">> baseObjectResult = ApiResponse.ok();");
		listImpl.addBodyLine("QueryWrapper<"+poClassName+"> queryWrapper = new QueryWrapper<>();");
		listImpl.addBodyLine("queryWrapper.eq(\"delFlag\",0);");
		listImpl.addBodyLine("List<"+poClassName+"> list = "+shortName+".selectList(queryWrapper);");
		listImpl.addBodyLine("List<"+resDtoName+"> thisDTOS = BeanMapper.mapList(list,"+resDtoName+".class);");
		listImpl.addBodyLine("baseObjectResult.setData(thisDTOS);");
		listImpl.addBodyLine("return baseObjectResult;");
		listImpl.setVisibility(JavaVisibility.PUBLIC);
		listImpl.setName("list");
		listImpl.setReturnType(new FullyQualifiedJavaType("ApiResponse<List<"+resDtoName+">>"));
		topLevelClass.addMethod(listImpl);


		Method findListImpl = new Method();
		findListImpl.addJavaDocLine("/**");
		findListImpl.addJavaDocLine(" * MBG.serviceImpl ");
		findListImpl.addJavaDocLine(" * 条件查询所有" );
		findListImpl.addJavaDocLine(" */");
		findListImpl.addAnnotation("@Override");
		findListImpl.addBodyLine("ApiResponse<List<"+resDtoName+">> baseObjectResult = ApiResponse.ok();");
		findListImpl.addBodyLine("if(null == vo){");
		findListImpl.addBodyLine("return list();");
		findListImpl.addBodyLine("}");
		findListImpl.addBodyLine("LambdaQueryWrapper<"+poClassName+"> queryWrapper = new LambdaQueryWrapper<>();");
		findListImpl.addBodyLine("findQueryWrapper(queryWrapper,vo);");
		findListImpl.addBodyLine("List<"+poClassName+"> list = "+shortName+".selectList(queryWrapper);");
		findListImpl.addBodyLine("List<"+resDtoName+"> thisDTOS = BeanMapper.mapList(list,"+resDtoName+".class);");
		findListImpl.addBodyLine("baseObjectResult.setData(thisDTOS);");
		findListImpl.addBodyLine("return baseObjectResult;");
		findListImpl.setVisibility(JavaVisibility.PUBLIC);
		findListImpl.setName("findList");
		findListImpl.addParameter(new Parameter(new FullyQualifiedJavaType(""+ queryDtoName), "vo"));
		findListImpl.setReturnType(new FullyQualifiedJavaType("ApiResponse<List<"+resDtoName+">>"));
		topLevelClass.addMethod(findListImpl);



		Method findPageImpl = new Method();
		findPageImpl.addJavaDocLine("/**");
		findPageImpl.addJavaDocLine(" * MBG.serviceImpl ");
		findPageImpl.addJavaDocLine(" * 分页查询" );
		findPageImpl.addJavaDocLine(" */");
		findPageImpl.addAnnotation("@Override");
		findPageImpl.addBodyLine("ApiResponse<PageInfo<"+resDtoName+">> baseObjectResult = ApiResponse.ok();");
		findPageImpl.addBodyLine("LambdaQueryWrapper<"+poClassName+"> queryWrapper = new LambdaQueryWrapper<>();");
		findPageImpl.addBodyLine("findQueryWrapper(queryWrapper,vo.getQueryModel());");
		findPageImpl.addBodyLine("PageHelper.startPage(vo.getPageNum(),vo.getPageSize());");
		findPageImpl.addBodyLine("List<"+poClassName+"> list = thisMapper.selectList(queryWrapper);");
		findPageImpl.addBodyLine("PageInfo pageInfo = new PageInfo<>(list);");
		findPageImpl.addBodyLine("List<"+resDtoName+"> thisDTOS = BeanMapper.mapList(list,"+resDtoName+".class);");
		findPageImpl.addBodyLine("pageInfo.setList(thisDTOS);");
		findPageImpl.addBodyLine("baseObjectResult.setData(pageInfo);");
		findPageImpl.addBodyLine("return baseObjectResult;");
		findPageImpl.setVisibility(JavaVisibility.PUBLIC);
		findPageImpl.setName("findPage");
		findPageImpl.addParameter(new Parameter(new FullyQualifiedJavaType("PageQuery<"+queryDtoName+">"), "vo"));
		//findPageImpl.addParameter(new Parameter(new FullyQualifiedJavaType("Integer"), "currentPage"));
		//findPageImpl.addParameter(new Parameter(new FullyQualifiedJavaType("Integer"), "pageSize"));
		findPageImpl.setReturnType(new FullyQualifiedJavaType("ApiResponse<PageInfo<"+resDtoName+">>"));
		topLevelClass.addMethod(findPageImpl);


		Method findExample = new Method();
		findExample = new Method();
		findExample.addJavaDocLine("/**");
		findExample.addJavaDocLine(" * MBG.find条件组装");
		findExample.addJavaDocLine(" * find条件组装" );
		findExample.addJavaDocLine(" */");

		findExample.addBodyLine("//queryWrapper.like(StringUtils.isNotBlank(vo.getIpaddr()), SysLogininfor::getIpaddr, sysLogininforVo.getIpaddr());");
		findExample.addBodyLine("//queryWrapper.eq(StringUtils.isNotBlank(vo.getStatus()), SysLogininfor::getStatus, sysLogininforVo.getStatus());");

		findExample.addBodyLine("queryWrapper.eq(UserAccount::getDelFlag,0);");


		findExample.setVisibility(JavaVisibility.PUBLIC);
		findExample.setName("findQueryWrapper");
		findExample.addParameter(new Parameter(new FullyQualifiedJavaType("LambdaQueryWrapper"), "queryWrapper"));
		findExample.addParameter(new Parameter(new FullyQualifiedJavaType(queryDtoName), "vo"));

		topLevelClass.addMethod(findExample);


		GeneratedJavaFile generatedJavaImplFile= new GeneratedJavaFile(topLevelClass, implTargetProject, javaFormatter);
		return generatedJavaImplFile;
	}


	/**
	 *client文件生成
	 * @return
	 */
	public GeneratedJavaFile generatedJavaClientFile(String queryDtoImport, String queryDtoName, String resDtoImport, String resDtoName, String facadeName, String interPage, String clientTargetProject
			, String clientTargetPackage, FullyQualifiedTable table, FullyQualifiedJavaType facadeType
			, IntrospectedTable introspectedTable, String poMapperInterFaceName, String poClassName, JavaFormatter javaFormatter){
		//######################################facade 实现类 ######################################
		interPage = clientTargetPackage+"."+table.getDomainObjectName().replace("DTO","")+"Client";
		FullyQualifiedJavaType typeImp = new FullyQualifiedJavaType(interPage);
		TopLevelClass topLevelClass = new TopLevelClass(typeImp);
		topLevelClass.setVisibility(JavaVisibility.PUBLIC);

		topLevelClass.addImportedType(facadeType);
		topLevelClass.addImportedType(new FullyQualifiedJavaType(resDtoImport));
		topLevelClass.addImportedType(new FullyQualifiedJavaType(queryDtoImport));
		topLevelClass.addImportedType("org.springframework.beans.factory.annotation.Autowired");
		topLevelClass.addImportedType(new FullyQualifiedJavaType("java.util.List"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("org.framework.cloud.core.result.*"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("org.framework.cloud.core.model.response.PageQueryResultBind"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("org.framework.cloud.core.lang.*"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("java.util.Date"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("org.springframework.web.bind.annotation.*"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("org.springframework.stereotype.Service"));

		topLevelClass.addJavaDocLine("/**");
		topLevelClass.addJavaDocLine(" * MBG.client ");
		topLevelClass.addJavaDocLine(" */");

		//topLevelClass.addAnnotation("@SysLog");
		topLevelClass.addAnnotation("@Service");

		FullyQualifiedJavaType parameterType =  new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
		topLevelClass.addImportedType(parameterType);

		//注入当前Facade
		Field field = new Field();
		field.addAnnotation("@Autowired");
		field.setVisibility(JavaVisibility.PRIVATE);
		field.setType(facadeType);
		String shortName = lowerFirst(facadeName);
		field.setName(shortName);
		topLevelClass.addField(field );

		topLevelClass.addImportedType(FullyQualifiedJavaType.getNewMapInstance());

		//新增方法
		Method createImpl = new Method();
		createImpl = new Method();
		createImpl.addJavaDocLine("/**");
		createImpl.addJavaDocLine(" * 新增" );
		createImpl.addJavaDocLine(" */");
		createImpl.addBodyLine("return "+shortName+".create(dto);");
		createImpl.setVisibility(JavaVisibility.PUBLIC);
		createImpl.setName("create");
		createImpl.addParameter(new Parameter(new FullyQualifiedJavaType(table.getDomainObjectName()), "dto"));
		createImpl.setReturnType(new FullyQualifiedJavaType("BindResult<"+resDtoName+">"));
		topLevelClass.addMethod(createImpl);

		//修改方法
		Method updateImpl = new Method();
		updateImpl = new Method();
		updateImpl.addJavaDocLine("/**");
		updateImpl.addJavaDocLine(" * 修改" );
		updateImpl.addJavaDocLine(" */");
		updateImpl.addBodyLine("return "+shortName+".update(dto);");
		updateImpl.setVisibility(JavaVisibility.PUBLIC);
		updateImpl.setName("update");
		updateImpl.addParameter(new Parameter(new FullyQualifiedJavaType(table.getDomainObjectName()), "dto"));
		updateImpl.setReturnType(new FullyQualifiedJavaType("BindResult<Void>"));
		topLevelClass.addMethod(updateImpl);

		//删除方法
		Method removeImpl = new Method();
		removeImpl = new Method();
		removeImpl.addJavaDocLine("/**");
		removeImpl.addJavaDocLine(" * 通过ID删除" );
		removeImpl.addJavaDocLine(" */");
		removeImpl.addBodyLine("return "+shortName+".remove(id,userName);");
		removeImpl.setVisibility(JavaVisibility.PUBLIC);
		removeImpl.setName("remove");
		removeImpl.addParameter(new Parameter(new FullyQualifiedJavaType("Integer"), "id"));
		removeImpl.addParameter(new Parameter(new FullyQualifiedJavaType("String"), "userName"));
		removeImpl.setReturnType(new FullyQualifiedJavaType("BindResult<Void>"));
		topLevelClass.addMethod(removeImpl);

		//通过ID查询
		Method getImpl = new Method();
		getImpl = new Method();
		getImpl.addJavaDocLine("/**");
		getImpl.addJavaDocLine(" * 通过ID查询" );
		getImpl.addJavaDocLine(" */");
		getImpl.addBodyLine("return "+shortName+".get(id);");
		getImpl.setVisibility(JavaVisibility.PUBLIC);
		getImpl.setName("get");
		getImpl.addParameter(new Parameter(new FullyQualifiedJavaType("Integer"), "id"));
		getImpl.setReturnType(new FullyQualifiedJavaType("BindResult<"+resDtoName+">"));
		topLevelClass.addMethod(getImpl);

		// /**
		//     * 查询所有费用项目列表
		Method listImpl = new Method();
		listImpl = new Method();
		listImpl.addJavaDocLine("/**");
		listImpl.addJavaDocLine(" * 查询所有" );
		listImpl.addJavaDocLine(" */");
		listImpl.addBodyLine("return "+shortName+".list();");
		listImpl.setVisibility(JavaVisibility.PUBLIC);
		listImpl.setName("list");
		listImpl.setReturnType(new FullyQualifiedJavaType("BindResult<List<"+resDtoName+">>"));
		topLevelClass.addMethod(listImpl);

		//条件查询所有
		Method findListImpl = new Method();
		findListImpl = new Method();
		findListImpl.addJavaDocLine("/**");
		findListImpl.addJavaDocLine(" * 条件查询所有" );
		findListImpl.addJavaDocLine(" */");
		findListImpl.addBodyLine("return "+shortName+".findList(dto);");
		findListImpl.setVisibility(JavaVisibility.PUBLIC);
		findListImpl.setName("findList");
		findListImpl.addParameter(new Parameter(new FullyQualifiedJavaType(queryDtoName), "dto"));
		findListImpl.setReturnType(new FullyQualifiedJavaType("BindResult<List<"+resDtoName+">>"));
		topLevelClass.addMethod(findListImpl);


		//分页查询
		Method findPageImpl = new Method();
		findPageImpl = new Method();
		findPageImpl.addJavaDocLine("/**");
		findPageImpl.addJavaDocLine(" * 分页查询" );
		findPageImpl.addJavaDocLine(" */");
		findPageImpl.addBodyLine("return "+shortName+".findPage(dto,pageNum,pageSize);");
		findPageImpl.setVisibility(JavaVisibility.PUBLIC);
		findPageImpl.setName("findPage");
		findPageImpl.addParameter(new Parameter(new FullyQualifiedJavaType(queryDtoName), "dto"));
		findPageImpl.addParameter(new Parameter(new FullyQualifiedJavaType("Integer"), "currentPage"));
		findPageImpl.addParameter(new Parameter(new FullyQualifiedJavaType("Integer"), "pageSize"));
		findPageImpl.setReturnType(new FullyQualifiedJavaType("BindResult<PageQueryResultBind<"+resDtoName+">>"));
		topLevelClass.addMethod(findPageImpl);

		GeneratedJavaFile generatedJavaClientFile= new GeneratedJavaFile(topLevelClass, clientTargetProject, javaFormatter);
		return generatedJavaClientFile;
	}


	/**
	 *Controller文件生成
	 * @return
	 */
	public GeneratedJavaFile generatedJavaControllerFile(String queryDtoImport, String queryDtoName, String resDtoImport, String resDtoName, String interPage, String controllerTargetProject
			, String controllerTargetPackage, FullyQualifiedTable table, String clientTargetPackage
			, IntrospectedTable introspectedTable, String poMapperInterFaceName, String poClassName, JavaFormatter javaFormatter){

		String servicePage = interPage;
		FullyQualifiedJavaType typeService = new FullyQualifiedJavaType(servicePage);

		String serviceName = table.getDomainObjectName().replace("DTO","").replace("Vo","")+"Service";

		//######################################Controller文件生成  ######################################
		interPage = controllerTargetPackage+"."+table.getDomainObjectName().replace("DTO","").replace("Vo","")+"Controller";
		FullyQualifiedJavaType typeImp = new FullyQualifiedJavaType(interPage);
		TopLevelClass interfaze = new TopLevelClass(typeImp);
		interfaze.setVisibility(JavaVisibility.PUBLIC);
		interfaze.addJavaDocLine("/**");
		interfaze.addJavaDocLine(" * MBG.api ");
		interfaze.addJavaDocLine(" */");

		interfaze.addAnnotation("@RestController");
		interfaze.addAnnotation("@RequestMapping(ConfigDomainConstants.API_PREFIX + \"/api\" + \""+"/"+lowerFirst(table.getDomainObjectName().replace("DTO","").replace("Vo",""))+"\")");
		interfaze.addAnnotation("@Api(tags = \""+introspectedTable.getRemarks()+"\")");

		FullyQualifiedJavaType parameterType =  new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
		interfaze.addImportedType(parameterType);

		interfaze.addImportedType(typeService);
		interfaze.addImportedType(new FullyQualifiedJavaType("io.swagger.annotations.Api"));
		interfaze.addImportedType(new FullyQualifiedJavaType("org.springframework.web.bind.annotation.RequestMapping"));
		interfaze.addImportedType(new FullyQualifiedJavaType(resDtoImport));
		interfaze.addImportedType(new FullyQualifiedJavaType(queryDtoImport));
		interfaze.addImportedType(new FullyQualifiedJavaType("org.springframework.beans.factory.annotation.Autowired"));
		interfaze.addImportedType(new FullyQualifiedJavaType("com.myt.service.infrastructure.constants.ConfigDomainConstants"));
		interfaze.addImportedType(new FullyQualifiedJavaType("java.util.List"));
		interfaze.addImportedType(new FullyQualifiedJavaType("javax.servlet.http.HttpServletRequest"));
		interfaze.addImportedType(new FullyQualifiedJavaType("io.swagger.annotations.ApiOperation"));
		interfaze.addImportedType(new FullyQualifiedJavaType("org.springframework.web.bind.annotation.*"));
		interfaze.addImportedType(new FullyQualifiedJavaType("com.github.pagehelper.PageInfo"));

		//校验包
		interfaze.addImportedType(new FullyQualifiedJavaType("com.myt.service.core.entity.ApiResponse"));

		//注入当前Client
		Field field = new Field();
		field.addAnnotation("@Autowired");
		field.setVisibility(JavaVisibility.PRIVATE);
		field.setType(typeService);
		String shortName = lowerFirst(serviceName);
		field.setName(shortName);
		interfaze.addField(field );

		//新增方法
		Method create = new Method();
		create.addJavaDocLine("/**");
		create.addJavaDocLine(" *  MBG.api ");
		create.addJavaDocLine(" * 新增" );
		create.addJavaDocLine(" */");
		create.addAnnotation("@PostMapping(\"/v1/create\")");
		create.addAnnotation("@ApiOperation(value = \"新增\")");
		create.setVisibility(JavaVisibility.PUBLIC);
		create.setName("create");
		create.addBodyLine("//TODO 定义创建人等信息");
		create.addBodyLine("return "+shortName+".create(vo);");
		create.addParameter(new Parameter(new FullyQualifiedJavaType("HttpServletRequest"), "req"));
		create.addParameter(new Parameter(new FullyQualifiedJavaType("@RequestBody "+ table.getDomainObjectName()), "vo"));
		create.setReturnType(new FullyQualifiedJavaType("ApiResponse"));
		interfaze.addMethod(create);

		//修改方法
		Method update = new Method();
		update.addJavaDocLine("/**");
		update.addJavaDocLine(" * MBG.api ");
		update.addJavaDocLine(" * 修改" );
		update.addJavaDocLine(" */");
		update.addAnnotation("@PostMapping(\"/v1/update\")");
		update.addAnnotation("@ApiOperation(value = \"修改【具体实现根据什么修改备注一下，方便调用者理解】\")");
		update.setVisibility(JavaVisibility.PUBLIC);
		update.setName("update");
		update.addBodyLine("//TODO 定义修改人等信息");
		//update.addBodyLine("dto.setUpdateTime(new Date());");
		update.addBodyLine("return "+shortName+".update(vo);");
		update.addParameter(new Parameter(new FullyQualifiedJavaType("HttpServletRequest"), "req"));
		update.addParameter(new Parameter(new FullyQualifiedJavaType("@RequestBody "+ table.getDomainObjectName()), "vo"));
		update.setReturnType(new FullyQualifiedJavaType("ApiResponse"));
		interfaze.addMethod(update);

		//删除方法
		Method remove = new Method();
		remove.addJavaDocLine("/**");
		remove.addJavaDocLine(" * MBG.api ");
		remove.addJavaDocLine(" * 通过主键Id删除" );
		remove.addJavaDocLine(" */");
		remove.addAnnotation("@PostMapping(\"/v1/delete\")");
		remove.addAnnotation("@ApiOperation(value = \"通过主键Id删除\")");
		remove.setVisibility(JavaVisibility.PUBLIC);
		remove.setName("delete");
		remove.addBodyLine("//TODO 定义修改人等信息");
		remove.addBodyLine("String updateBy = \"张三\";");
		remove.addBodyLine("return "+shortName+".delete(id,updateBy);");
		remove.addParameter(new Parameter(new FullyQualifiedJavaType("HttpServletRequest"), "req"));
		remove.addParameter(new Parameter(new FullyQualifiedJavaType("@RequestParam Integer"), "id"));
		remove.setReturnType(new FullyQualifiedJavaType("ApiResponse"));
		interfaze.addMethod(remove);

		//通过ID查询
		Method get = new Method();
		get.addJavaDocLine("/**");
		get.addJavaDocLine(" * MBG.api ");
		get.addJavaDocLine(" * 通过主键Id查询" );
		get.addJavaDocLine(" */");
		get.addAnnotation("@GetMapping(\"/v1/getById\")");
		get.addAnnotation("@ApiOperation(value = \"通过主键Id查询\")");
		get.setVisibility(JavaVisibility.PUBLIC);
		get.setName("getById");
		get.addBodyLine("return "+shortName+".getById(id);");
		get.addParameter(new Parameter(new FullyQualifiedJavaType("@RequestParam Integer"), "id"));
		get.setReturnType(new FullyQualifiedJavaType("ApiResponse<"+resDtoName+">"));
		interfaze.addMethod(get);

		//查询所有
//		Method list = new Method();
//		list.addJavaDocLine("/**");
//		list.addJavaDocLine(" * MBG.api ");
//		list.addJavaDocLine(" * 查询所有" );
//		list.addJavaDocLine(" */");
//		list.addAnnotation("@GetMapping(\"/list\")");
//		list.addAnnotation("@ApiOperation(value = \"查询所有\")");
//		list.setVisibility(JavaVisibility.PUBLIC);
//		list.setName("list");
//		list.addBodyLine(" return "+shortName+".list();");
//		list.setReturnType(new FullyQualifiedJavaType("BaseListResult<"+resDtoName+">"));
//		interfaze.addMethod(list);

		//条件查询所有
		Method findList = new Method();
		findList.addJavaDocLine("/**");
		findList.addJavaDocLine(" * MBG.api ");
		findList.addJavaDocLine(" * 条件查询所有" );
		findList.addJavaDocLine(" */");
		findList.addAnnotation("@PostMapping(\"/v1/findList\")");
		findList.addAnnotation("@ApiOperation(value = \"条件查询所有\")");
		findList.setVisibility(JavaVisibility.PUBLIC);
		findList.setName("findList");
		findList.addBodyLine(" return "+shortName+".findList(vo);");
		findList.addParameter(new Parameter(new FullyQualifiedJavaType("@RequestBody "+ queryDtoName), "vo"));
		//findList.setReturnType(new FullyQualifiedJavaType("ApiResponse"));
		findList.setReturnType(new FullyQualifiedJavaType("ApiResponse<List<"+resDtoName+">>"));
		interfaze.addMethod(findList);


		//分页查询
		Method findPage = new Method();
		findPage.addJavaDocLine("/**");
		findPage.addJavaDocLine(" * MBG.api ");
		findPage.addJavaDocLine(" * 条件分页查询" );
		findPage.addJavaDocLine(" */");
		findPage.addAnnotation("@PostMapping(\"/v1/findPage\")");
		findPage.addAnnotation("@ApiOperation(value = \"条件分页查询\")");
		findPage.setVisibility(JavaVisibility.PUBLIC);
		findPage.setName("findPage");
		findPage.addBodyLine(" return "+shortName+".findPage(vo);");
		findPage.addParameter(new Parameter(new FullyQualifiedJavaType("@RequestBody PageQuery<"+queryDtoName+">"), "vo"));
		//findPage.addParameter(new Parameter(new FullyQualifiedJavaType("@RequestParam Integer"), "currentPage"));
		//findPage.addParameter(new Parameter(new FullyQualifiedJavaType("@RequestParam Integer"), "pageSize"));
		//findPage.setReturnType(new FullyQualifiedJavaType("ApiResponse"));
		findPage.setReturnType(new FullyQualifiedJavaType("ApiResponse<PageInfo<"+resDtoName+">>"));
		interfaze.addMethod(findPage);

		GeneratedJavaFile generatedJavaClientFile= new GeneratedJavaFile(interfaze, controllerTargetProject, javaFormatter);
		return generatedJavaClientFile;
	}

	/**
	 * copy一个DTO 到DTO相同路径的response目录
	 */
	public GeneratedJavaFile copyDTOResponse(IntrospectedTable introspectedTable, GeneratedJavaFile generatedJavaFile
			, String targetProject, String resDtoModelTargetPackage, String resDtoName
			, String tableDomainObjectName, JavaFormatter javaFormatter){

		TopLevelClass tempTop = (TopLevelClass)generatedJavaFile.getCompilationUnit();

		String interPage = resDtoModelTargetPackage+"."+resDtoName;
		FullyQualifiedJavaType typeImp = new FullyQualifiedJavaType(interPage);

		TopLevelClass topLevelClass = new TopLevelClass(typeImp);
		topLevelClass.setVisibility(JavaVisibility.PUBLIC);
		topLevelClass.addSuperInterface(new FullyQualifiedJavaType("java.io.Serializable"));
		topLevelClass.addJavaDocLine("/**");
		topLevelClass.addJavaDocLine(" * MBG.ResDTO 数据表：" + introspectedTable.getFullyQualifiedTable().getIntrospectedTableName() + "(" + introspectedTable.getRemarks() + ")");
		topLevelClass.addJavaDocLine(" * Tool generation time " + (new Date()).toString());
		topLevelClass.addJavaDocLine(" */");
		topLevelClass.addAnnotation("@Data");
		topLevelClass.addImportedType("lombok.Data");
		topLevelClass.addAnnotation("@ApiModel(value = \"" + introspectedTable.getRemarks() + ".Res" + "\")");
		topLevelClass.addImportedType("java.time.LocalDateTime");
		topLevelClass.addImportedType("java.time.LocalDate");
		topLevelClass.addImportedType("java.io.Serializable");
		topLevelClass.addImportedType("io.swagger.annotations.ApiModel");
		topLevelClass.addImportedType("io.swagger.annotations.ApiModelProperty");

		for (Field field : tempTop.getFields()) {
			topLevelClass.addField(field);
		}

		GeneratedJavaFile generatedJavaFile1 = new GeneratedJavaFile(topLevelClass,targetProject,javaFormatter);

		return generatedJavaFile1;
	}

	/**
	 * copy一个DTO 到DTO相同路径的Query目录
	 */
	public GeneratedJavaFile copyDtoQuery(IntrospectedTable introspectedTable, GeneratedJavaFile generatedJavaFile
			, String targetProject, String queryDtoModelTargetPackage, String queryDtoName
			, JavaFormatter javaFormatter){

		//需要排除的字段
		List<String> excludeField = Arrays.asList("rowId","version","deleteFlag","createTime","createBy","updateTime","updateBy");

		TopLevelClass tempTop = (TopLevelClass)generatedJavaFile.getCompilationUnit();

		String interPage = queryDtoModelTargetPackage+"."+queryDtoName;
		FullyQualifiedJavaType typeImp = new FullyQualifiedJavaType(interPage);

		TopLevelClass topLevelClass = new TopLevelClass(typeImp);
		topLevelClass.setVisibility(JavaVisibility.PUBLIC);
		topLevelClass.addSuperInterface(new FullyQualifiedJavaType("java.io.Serializable"));
		topLevelClass.addJavaDocLine("/**");
		topLevelClass.addJavaDocLine(" * MBG.queryDTO 数据表：" + introspectedTable.getFullyQualifiedTable().getIntrospectedTableName() + "(" + introspectedTable.getRemarks() + ")");
		topLevelClass.addJavaDocLine(" * Tool generation time " + (new Date()).toString());
		topLevelClass.addJavaDocLine(" */");
		topLevelClass.addAnnotation("@Data");
		topLevelClass.addImportedType("lombok.Data");
		topLevelClass.addAnnotation("@ApiModel(value = \"" + introspectedTable.getRemarks()+".Query" + "\")");
		topLevelClass.addImportedType("java.time.LocalDateTime");
		topLevelClass.addImportedType("java.time.LocalDate");
		topLevelClass.addImportedType("java.io.Serializable");
		topLevelClass.addImportedType("io.swagger.annotations.ApiModel");
		topLevelClass.addImportedType("io.swagger.annotations.ApiModelProperty");

		for (Field field : tempTop.getFields()) {
			if(!excludeField.contains(field.getName())){
				//topLevelClass.addField(field);
			}
		}


//		Field fieldCurrentPage = new Field();
//		fieldCurrentPage.addAnnotation("@ApiModelProperty(value = \"当前页码\")");
//		fieldCurrentPage.setVisibility(JavaVisibility.PRIVATE);
//		fieldCurrentPage.setType(FullyQualifiedJavaType.getIntInstance());
//		fieldCurrentPage.setName("currentPage");
//		topLevelClass.addField(fieldCurrentPage);
//
//		Field fieldPageSize = new Field();
//		fieldPageSize.addAnnotation("@ApiModelProperty(value = \"页面大小\")");
//		fieldPageSize.setVisibility(JavaVisibility.PRIVATE);
//		fieldPageSize.setType(FullyQualifiedJavaType.getIntInstance());
//		fieldPageSize.setName("pageSize");
//		topLevelClass.addField(fieldPageSize);


		GeneratedJavaFile generatedJavaFile1 = new GeneratedJavaFile(topLevelClass,targetProject,javaFormatter);

		return generatedJavaFile1;
	}


	public static String lowerFirst(String oldStr){
		char[]chars = oldStr.toCharArray();
		chars[0] += 32;
		return String.valueOf(chars);
	}

	public static boolean haveFile(String targetProject,String page){
		String path = targetProject + "\\" + page.replace(".","\\")+".java";
		File file=new File(path);
		if(!file.exists()) {
			return false;
		}else return true;
	}


	private static void copyFileUsingFileStreams(File source, File dest)
			throws IOException {
		InputStream input = null;
		OutputStream output = null;
		try {
			input = new FileInputStream(source);
			output = new FileOutputStream(dest);
			byte[] buf = new byte[1024];
			int bytesRead;
			while ((bytesRead = input.read(buf)) > 0) {
				output.write(buf, 0, bytesRead);
			}
		} finally {
			input.close();
			output.close();
		}
	}
}
